#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<errno.h>

int main()
{
    pid_t id = fork();
    if(id == 0)
    {
        int cnt = 3;
        while(1)
        {
            printf("我是一个子进程，pid: %d, ppid: %d\n", getpid(), getppid());
            sleep(1);
            cnt--;
        }
        exit(10);
    }
    // 父进程
    // 1.子进程退出
    // 2.子进程没退出呢？
    sleep(10);
    //pid_t rid = wait(NULL);
    //pid_t rid = waitpid(-1, NULL, 0);
    //pid_t rid = waitpid(id-1, NULL, 0);
    int status = 0;
    pid_t rid = waitpid(id, &status, 0);
    if(rid > 0)
    {
        printf("wait success, rid: %d, status: %d, exit signal: %d\n", rid, (status>>8)&0xFF, status&0xF); // 0xFF前24个为0，后8个为1
    }
    else
    {
        printf("wait fail: %d: %s\n", errno, strerror(errno));
    }
    sleep(5);
    return 0;
}



